package com.yx.cloud.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.iot.product.sweeper.bean.Consumption;
import com.iot.product.sweeper.config.SweeperProductConfig;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.ConsumablesmaintainActivityBinding;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

/**
 * 耗材与保养界面
 */
public class ConsumablesMaintainActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "ConsumablesMaintainActivity";

    private String workTime_filter, workTime_sideBrush, workTime_mainBrush, workTime_mop, workTime_waterTankFilter, workTime_dustBag;
    private Robot robot;
    private ConsumablesmaintainActivityBinding binding;

    @Override
    public void onCreate() {
        binding = ConsumablesmaintainActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @SuppressLint("LongLogTag")
    private void initData() {
        robot = AppContext.get().getCurrentDevice();
        JSONObject productConfig = AppContext.getConfig();
        Consumption consumption = robot.getConsumption();
        LogUtils.i("Consumption : " + consumption);
        if (productConfig.optInt(ProductConfigConstants.consumable_sideBrush, -1) == -1) {
            binding.rlConsunablesSidebrush.setVisibility(View.GONE);
        } else {
            binding.rlConsunablesSidebrush.setVisibility(View.VISIBLE);
            float totalLife = productConfig.optInt(ProductConfigConstants.consumable_sideBrush, -1);
            float remain = consumption.getSideBrushLife();
            int percent = (int) (remain > totalLife ? 100 : remain * 100 / totalLife);
            binding.tvSideBrush.setText(percent + "");//边刷
            String result = getResources().getString(R.string.MO_Remain_Tip).replace("{X}", (int) (remain / 3600 + 0.5f) + "");
            binding.tvSideBrushSurplusTime.setText(result);
            binding.pbConsunablesSidebrush.setProgress(percent);

            workTime_sideBrush = (int) (totalLife - remain) / 3600 + "";
        }

        if (productConfig.optInt(ProductConfigConstants.consumable_mainBrush, -1) == -1) {
            binding.rlConsunablesBrush.setVisibility(View.GONE);
        } else {
            binding.rlConsunablesBrush.setVisibility(View.VISIBLE);
            float totalLife = productConfig.optInt(ProductConfigConstants.consumable_mainBrush, -1);
            float remain = consumption.getMainBrushLife();
            int percent = (int) (remain > totalLife ? 100 : remain * 100 / totalLife);
            binding.tvMainBrush.setText(percent + "");//主刷
            String result = getResources().getString(R.string.MO_Remain_Tip).replace("{X}", (int) (remain / 3600 + 0.5f) + "");
            binding.tvMainBrushSurplusTime.setText(result);
            binding.pbConsunablesBrush.setProgress(percent);

            workTime_mainBrush = (int) (totalLife - remain) / 3600 + "";
        }

        if (productConfig.optInt(ProductConfigConstants.consumable_dustStrainer, -1) == -1) {
            binding.rlConsunablesStrainer.setVisibility(View.GONE);
        } else {
            binding.rlConsunablesStrainer.setVisibility(View.VISIBLE);
            float totalLife = productConfig.optInt(ProductConfigConstants.consumable_dustStrainer, -1);
            float remain = consumption.getStrainerLife();
            int percent = (int) (remain > totalLife ? 100 : remain * 100 / totalLife);
            binding.tvFilter.setText(percent + "");//尘盒滤网
            String result = getResources().getString(R.string.MO_Remain_Tip).replace("{X}", (int) (remain / 3600 + 0.5f) + "");
            binding.tvFilterSurplusTime.setText(result);
            binding.pbConsunablesStrainer.setProgress(percent);

            workTime_filter = (int) (totalLife - remain) / 3600 + "";
        }


        if (productConfig.optInt(ProductConfigConstants.consumable_mop, -1) == -1) {
            binding.rlConsunablesMopBag.setVisibility(View.GONE);
        } else {
            binding.rlConsunablesMopBag.setVisibility(View.VISIBLE);
            float totalLife = productConfig.optInt(ProductConfigConstants.consumable_mop, -1);
            float remain = consumption.getMopLife();
            int percent = (int) (remain > totalLife ? 100 : remain * 100 / totalLife);
            binding.tvMop.setText(percent + "");//拖布
            String result = getResources().getString(R.string.MO_Remain_Tip).replace("{X}", (int) (remain / 3600 + 0.5f) + "");
            binding.tvMopSurplusTime.setText(result);
            binding.pbConsunablesMopBag.setProgress(percent);

            workTime_mop = (int) (totalLife - remain) / 3600 + "";
        }

        if (productConfig.optInt(ProductConfigConstants.consumable_waterStrainer, -1) == -1) {
            binding.rlConsunablesWaterTankFilter.setVisibility(View.GONE);
        } else {
            binding.rlConsunablesWaterTankFilter.setVisibility(View.VISIBLE);
            float totalLife = productConfig.optInt(ProductConfigConstants.consumable_waterStrainer, -1);
            float remain = consumption.getWaterTankFilterLife();

            LogUtils.i("--扫拖模块滤网--222--" + remain);

            int percent = (int) (remain > totalLife ? 100 : remain * 100 / totalLife);
            binding.tvWaterTankFilter.setText(percent + "");//扫拖模块滤网
            String result = getResources().getString(R.string.MO_Remain_Tip).replace("{X}", (int) (remain / 3600 + 0.5f) + "");
            binding.tvWaterTankFilterTime.setText(result);
            binding.pbConsunablesWaterTankFilter.setProgress(percent);

            workTime_waterTankFilter = (int) (totalLife - remain) / 3600 + "";
        }

        if (productConfig.optInt(ProductConfigConstants.consumable_dustBag, -1) == -1) {
            binding.rlConsunablesReplaceBag.setVisibility(View.GONE);
        } else {
            binding.rlConsunablesReplaceBag.setVisibility(View.VISIBLE);
            float totalLife = productConfig.optInt(ProductConfigConstants.consumable_waterStrainer, -1);
            float remain = consumption.getWaterTankFilterLife();
            int percent = (int) (remain > totalLife ? 100 : remain * 100 / totalLife);
            binding.tvDustBag.setText(percent + "");//集尘袋
            String result = getResources().getString(R.string.MO_Remain_Tip).replace("{X}", (int) (remain / 3600 + 0.5f) + "");
            binding.tvDustBagSurplusTime.setText(result);
            binding.tvConsunablesReplaceBag.setProgress(percent);
            workTime_dustBag = (int) (totalLife - remain) / 3600 + "";
        }

        binding.ivConsumablesReturn.setOnClickListener(this);
        binding.rlConsunablesStrainer.setOnClickListener(this);
        binding.rlConsunablesSidebrush.setOnClickListener(this);
        binding.rlConsunablesBrush.setOnClickListener(this);
        binding.rlConsunablesReplaceBag.setOnClickListener(this);
        binding.rlConsunablesMopBag.setOnClickListener(this);
        binding.rlConsunablesWaterTankFilter.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        Intent intent = new Intent(this, ConsumableMaintenanceInformationActivty.class);
        switch (v.getId()) {
            case R.id.iv_consumablesReturn:
                Log.i(TAG, "--right----onViewClicked");
                Intent_utils.backIntent(this);
                break;
            case R.id.rl_consunablesStrainer:
                //滤网
                intent.putExtra("consunables", "Strainer");
                intent.putExtra("workTime", workTime_filter);
                intent.putExtra("surplusTime", binding.tvFilter.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.rl_consunablesSidebrush:
                //边刷
                intent.putExtra("consunables", "Sidebrush");
                intent.putExtra("workTime", workTime_sideBrush);
                intent.putExtra("surplusTime", binding.tvSideBrush.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.rl_consunablesBrush:
                //主刷
                intent.putExtra("consunables", "Brush");
                intent.putExtra("workTime", workTime_mainBrush);
                intent.putExtra("surplusTime", binding.tvMainBrush.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.rl_consunablesMop_bag:
                //拖布
                intent.putExtra("consunables", "mop");
                intent.putExtra("workTime", workTime_mop);
                intent.putExtra("surplusTime", binding.tvMop.getText().toString().trim());
                startActivity(intent);
                break;

            case R.id.rl_consunables_WaterTankFilter:
                //水箱滤网
                intent.putExtra("consunables", "Water");
                intent.putExtra("workTime", workTime_waterTankFilter);
                intent.putExtra("surplusTime", binding.tvWaterTankFilter.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.rl_consunablesReplace_bag:
                //集尘
                intent.putExtra("consunables", "bag");
                intent.putExtra("workTime", workTime_dustBag);
                intent.putExtra("surplusTime", binding.tvDustBag.getText().toString().trim());
                startActivity(intent);
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_GET_CONSUMPTION:
                if (event.isResult()) {
                    initData();
                }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
